package com.dly.hope.controller;

import com.dly.hope.annotation.LoginRole;
import com.dly.hope.constant.R;
import com.dly.hope.model.ro.BaseRo;
import com.dly.hope.model.ro.LoginRo;
import com.dly.hope.model.ro.PromiseRo;
import com.dly.hope.model.ro.RegisterRo;
import com.dly.hope.model.type.PromiseType;
import com.dly.hope.model.vo.LoginVo;
import com.dly.hope.model.vo.OnLineVo;
import com.dly.hope.model.vo.UserPromiseVo;
import com.dly.hope.model.vo.UserVo;
import com.dly.hope.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author linyu.dai
 * @project myserve
 * @description 用户账号相关
 * @creat 2022/8/27 19:13:41
 */
@RestController
@Api(tags = "用户相关")
@Slf4j
public class UserAccountController extends BaseController{

    @Resource
    private UserAccountService userAccountService;

    @ApiOperation(value = "用户登入/email")
    @PostMapping("/login")
    public ResponseEntity<R<LoginVo>> login(@RequestBody @Valid LoginRo loginRo, BindingResult br){
        return operate(()->this.successDate(userAccountService.login(loginRo)), br);
    }


    @ApiOperation(value = "用户注册/通过邮箱")
    @PostMapping("/register")
    public ResponseEntity<R<Void>> register(@RequestBody @Validated(value = BaseRo.WhenCheckLoginCode.class) RegisterRo registerRo, BindingResult br){
        validateFiled(br);
        userAccountService.register(registerRo);
        return operate(this::success);
    }

    @ApiOperation(value = "发送验证码")
    @GetMapping("/sendCode")
    public ResponseEntity<R<Void>> send(@RequestParam String email){
        userAccountService.sendCode(email);
        return operate(this::success);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/modifyPassword")
    public ResponseEntity<R<Void>> modifyPassword(@RequestBody @Validated(value = BaseRo.WhenModifyPassword.class) RegisterRo registerRo, BindingResult br){
        validateFiled(br);
        userAccountService.modifyPassword(registerRo);
        return operate(this::success);
    }

    @ApiOperation(value = "获取在线信息")
    @GetMapping("/getOnLinePerPon")
    public ResponseEntity<R<OnLineVo>> getOnLinePerPon() {
        return operate(() -> this.successDate(userAccountService.getOnLinePeople()));
    }

    @ApiOperation(value = "保持链接")
    @GetMapping("/ping")
    @LoginRole
    public ResponseEntity<R<Void>> ping(){
        log.info("用户{} -------ping",currentUser().getId());
        return operate(this::success);
    }

    @ApiOperation(value = "获取其他用户信息")
    @GetMapping("/getUser")
    @LoginRole
    public ResponseEntity<R<UserVo>> getUserAccount(String userId){
        return operate(()-> this.successDate(userAccountService.getUserAccount(userId)));
    }

    @ApiOperation(value = "设置用户头像")
    @PostMapping("/setAvatar")
    @LoginRole
    public ResponseEntity<R<Void>> setAvatar(@RequestParam String fileId){
        userAccountService.setAvatar(getCurrentUserId(), fileId);
        return operate(this::success);
    }

    @ApiOperation(value = "设置用户名字")
    @PostMapping("/setName")
    @LoginRole
    public ResponseEntity<R<Void>> setName(String nowName){
        userAccountService.setName(getCurrentUserId(), nowName);
        return operate(this::success);
    }

    @ApiOperation(value = "设置一些用户习惯")
    @PostMapping("/setPromise")
    @LoginRole
    public ResponseEntity<R<Void>> setPromise(@RequestBody @Validated PromiseRo promiseRo){
        userAccountService.setPromise(promiseRo, getCurrentUserId());
        return operate(this::success);
    }

    @ApiOperation(value = "获取用户的习惯")
    @GetMapping("/getPromise")
    @LoginRole
    public ResponseEntity<R<UserPromiseVo>> getUserPromise(){
        return operate(()->this.successDate(userAccountService.getPromise(getCurrentUserId())));
    }
}

